﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentScheduler;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Business;
using YiYiTong.Rocket.Model;
using YiYiTong.Rocket.EFDBFirst;

namespace YiYiTong.Rocket.ProcessConsole
{
    /// <summary>
    /// 订单支付超时处理
    /// </summary>
    public class OrderOverTimeJob : BaseJob
    {
        public override void DoAction()
        {
            //取出所有未支付订单
            Tuple<bool, OrderListModel> result = OrderBiz.Instance.GetOrderList(-10, 0, 1, 1000000);
            if (result.Item1)
            {
                foreach (var item in result.Item2.list)
                {
                    //支付已超时，处理库存
                    if (item.pay_end_time != DateTime.MinValue && item.pay_end_time < DateTime.Now && item.pay_end_time > new DateTime(2019, 1, 1))
                    {
                        if (item.goodlist != null)
                        {
                            foreach (var item2 in item.goodlist)
                            {
                                //开始恢复库存
                                if (!GoodsBiz.Instance.AddStockAmount(item2.goods_id, item2.buy_amount))
                                {
                                    LogHelper.Error("订单支付超时处理-恢复库存失败," + JsonHelper.Serialize(item2));
                                }
                            }
                        }
                        //修改订单状态
                        OrderBiz.Instance.UpdateOrderStatus(item.order_NO, (int)item.order_status, (int)OrderStatus.Cancel);
                        try
                        {
                            //支付订单取消的话竞拍订单也要取消
                            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
                            var jingjia_submit = dbcontext.ADZ_JingjiaSubmit.FirstOrDefault(t => t.order_master_id == item.order_id);
                            if (jingjia_submit != null && jingjia_submit.status == 2)
                            {
                                jingjia_submit.is_hit = 0;
                                dbcontext.SaveChanges();
                            }

                            //发送推送消息
                            AddPushMsgModel paramModel = new AddPushMsgModel
                            {
                                aDZ_Pushmessage = new ADZ_Pushmessage
                                {
                                    MsgType = 2,
                                    ReceiveType = 1,
                                    MsgTitle = "订单关闭",
                                    MsgText = $"您的订单{item.order_NO}因超时未支付，订单已关闭。"
                                },
                                ReceiveUids = item.uid.ToString()
                            };
                            PushMsgBiz.Instance.AddPushMsg(paramModel);

                            OrderBiz.Instance.WriteOrderLog(new ADZ_OrderMasterLog
                            {
                                order_id = item.order_id,
                                opmsg = "订单支付超时，自动关闭"
                            });

                        }
                        catch (Exception ex)
                        {
                            LogHelper.Fatal("处理异常：" + ex.GetExcetionMessage());
                        }
                    }
                }
            }
        }
    }
}
